﻿using System;
using DevExpress.XtraEditors.DXErrorProvider;
using PMS.Resources;
using PMS.BLL;
using PMS.Entity;
using PMS.Utils;

namespace PMS.ValidationRules
{
    class DongMoKyValidateRules : ConditionValidationRule
    {

        public string DongMoKyNotExisted = "DongMoKyNotExisted";
        private bool Require = false;
        public DongMoKyValidateRules(bool require = false)
        {
            Require = require;
        }

        DongMoKyBO Service = new DongMoKyBO();
        public override bool Validate(System.Windows.Forms.Control control, object value)
        {
            try
            {

                string ProductCode = DataConvertUltils.ToString(value);
                DongMoKy emp = new DongMoKy();

                if (!string.IsNullOrEmpty(ProductCode))
                {
                    emp = Service.GetDongMoKyByID(ProductCode);
                    if (emp == null)
                    {
                        ErrorText = Messages.GetString(DongMoKyNotExisted);
                        return false;
                    }


                }
                else
                {
                    if (Require)
                    {
                        ErrorText = Messages.GetString(Constants.REQUIRE_FIELD);
                        return false;
                    }
                }

                return true;

            }
            catch
            {
                return true;
                //throw new Exception(ex.Message);
            }
        }
    }
}
